import { observable, action } from 'mobx';
import * as  Apis from '../apis/govern';
import { Modal} from 'antd';
import { WeaLocaleProvider } from "ecCom"
const getLabel = WeaLocaleProvider.getLabel;


export default class ImportStore {
    title = getLabel(389701, "事项导入");

    @observable type = "0";
    @observable categoryId = "";
    @observable excelId = "";
    @observable excelfile = "";
    @observable filelist = [];
    @observable replaceDatas = [];
    @observable spinning = false;
    @observable mandstr = "";
    @observable otherstr = "";
    @observable importmsg = "";
    @observable reflesh = 0;
    @observable msgStatus = false;
    @observable path = "";

    @action
    initDatas = (params = {}) => {
        this.loading = true;
        Apis.getBatchImportInfo(params).then(data => {
            this.excelId = "ExcelFile_govern_" + this.type + "_" + this.categoryId;
            this.mandstr = data.mandstr || "";
            this.otherstr = data.otherstr || "";
            this.importmsg = "";
        })
    }

    @action
    regenTemplate = () => {
        this.isrefresh = new Date().getTime();
        this.spinning = true;
        if(this.categoryId==""){
            Modal.warning({
                title: getLabel(127354, "警告"),
                content: getLabel('501390','未选择督办类型') ,
            });
            this.spinning = false;
            return;
        }
        Apis.getBatchImportInfo({ type: this.type, categoryId: this.categoryId }).then(data => {
            this.spinning = false;
            this.excelId = "ExcelFile_govern_" + this.type + "_" + this.categoryId;
            this.mandstr = data.mandstr;
            this.otherstr = data.otherstr;
            this.importmsg = "";
            this.excelfile = "";
            this.filelist = [];
            Modal.success({
                title: getLabel(128231, "模板创建完成，可以下载了。"),
                content: '',
            });
        })
    }

    @action
    doImport = () => {
        if(this.categoryId==""){
            Modal.warning({
                title: getLabel(127354, "警告"),
                content: getLabel('501390','未选择督办类型') ,
            });
            this.spinning = false;
            return;
        }

        if (this.excelfile == '') {
            Modal.warning({
                title: getLabel(127354, "警告"),
                content: getLabel(126186, "未") + getLabel(82694, "选择Excel文件"),
            });
            return;
        } else {
            let _this = this;
            let params = { type: this.type, categoryId: this.categoryId, excelfile: this.excelfile || '' };
            this.spinning = true;

            Apis.doImport(params).then(data0 => {
                if (data0.status=="1") {
                    Apis.getImportErrorMsg().then(data => {
                        if (data) {
                             _this.path = data.path;
                            let msglist = data.msg || [];
                            if(msglist.length==0){
                                Modal.success({
                                    title: getLabel(28450, "导入成功！"),
                                    content: '',
                                });
                            }else{
                                let msg = '</br>';
                                for(var c in msglist){
                                    if(typeof msglist[c] === "string" &&  msglist[c].length !== 0 ){
                                        msg +=msglist[c]+'</br>';
                                    }
                                }
                                this.importmsg = msg;                                
                            }
                            this.spinning = false;
                        }
                    })
                }
            })
        }
    }

    @action
    saveCategory = (ids, data) => {
        this.type = ids ? "1" : "0";
        if (ids != this.categoryId) {
            this.excelfile = "";
            this.filelist = [];
            this.importmsg = "";
            this.path = "";
        }
        this.categoryId = ids;
        this.replaceDatas = data;
    }

}
